//
//  SlidableView.m
//  TanTanDemo
//
//  Created by wei1 on 2016/12/2.
//  Copyright © 2016年 qge. All rights reserved.
//

#import "SlidableView.h"
#import "DragableView.h"
#import "CornerBtn.h"
#define dragViewWidth 250
#define dragViewHeight 400
#define dragViewOffsetY 50
#define btnOffset 60
#define cornerBtnWH 50
#define cornBtnOffsetY dragViewHeight+dragViewOffsetY+10
#define ScreenH [UIScreen mainScreen].bounds.size.height
#define ScreenW [UIScreen mainScreen].bounds.size.width

@interface SlidableView()<DragebleViewDelegate>

@property (nonatomic) NSMutableArray<DragableView *>* totalViews;
@property (nonatomic) CornerBtn *dislikeBtn;
@property (nonatomic) CornerBtn *likeBtn;
@end

@implementation SlidableView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        [super layoutSubviews];
        [self setUpBtn];
    }
    return self;
}

-(void)setUpBtn{
    CornerBtn *dislikeBtn = [[CornerBtn alloc] initWithFrame:CGRectMake(btnOffset, cornBtnOffsetY, cornerBtnWH, cornerBtnWH)];
    _dislikeBtn = dislikeBtn;
    [dislikeBtn setImage:[UIImage imageNamed:@"home-user-dislike-button_64x64_"] forState:UIControlStateNormal];
    [self addSubview:dislikeBtn];
    [dislikeBtn addTarget:self action:@selector(swide2Left) forControlEvents:UIControlEventTouchUpInside];
    CornerBtn *likeBtn = [[CornerBtn alloc] initWithFrame:CGRectMake(ScreenW-btnOffset-cornerBtnWH , cornBtnOffsetY, cornerBtnWH, cornerBtnWH)];
    _likeBtn = likeBtn;
    [likeBtn setImage:[UIImage imageNamed:@"home-user-like-button_64x64_"] forState:UIControlStateNormal];
    [self addSubview:likeBtn];
    [likeBtn addTarget:self action:@selector(swide2Right) forControlEvents:UIControlEventTouchUpInside];
}

#pragma totalViews懒加载
-(NSMutableArray<DragableView *> *)totalViews{
    if (_totalViews == nil){
        _totalViews = [NSMutableArray array];
    }
    return _totalViews;
}

-(void)addViews:(NSArray<UIView *> *)views{
    for (UIView* view in views){
        DragableView *dragV = [[DragableView alloc]initWithFrame:CGRectMake((ScreenW - dragViewWidth)/2, dragViewOffsetY, dragViewWidth, dragViewHeight) andView:view];
        if (self.totalViews.lastObject == nil){
            [self addSubview:dragV];
        }else{
            [self insertSubview:dragV belowSubview:self.totalViews.lastObject];
        }
        dragV.delegate = self;
        [self.totalViews addObject:dragV];
    }
}
#pragma mark DragebleViewDelegate
-(void)dragableViewDragToLeft:(DragableView *)dragableView{
    NSLog(@"no");
    [self.totalViews removeObject:dragableView];
    [self checkTotalViewsCount];
}

-(void)dragableViewDragToRight:(DragableView *)dragableView{
    NSLog(@"yes");
    [self.totalViews removeObject:dragableView];
    [self checkTotalViewsCount];
}

-(void)checkTotalViewsCount{
    if (self.totalViews.count < 5){
        NSLog(@"请求数据");
    }
}
#pragma mark btnTarget

-(void)swide2Left{
    DragableView *view = self.totalViews.firstObject;
    if (view != nil){
        [view dragToLeft];
    }
}

-(void)swide2Right{
    DragableView *view = self.totalViews.firstObject;
    if (view != nil){
        [view dragToRight];
    }
}



@end
